
/*
 * Copyright 2013 Wajdi Hh "wajdihh@gmail.com".
 * 
 * BASED ON THIS PROJECT :https://code.google.com/p/color-picker-view/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hh.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hh.droid.R;
import com.hh.features.PfKeyboard;
import com.hh.listeners.OnColorPickChangedListener;
import com.hh.listeners.OnPickColorListener;
import com.hh.ui.UiUtility;
import com.hh.ui.view.ColorPickerView;
import com.hh.ui.view.ColorPickerView.OnColorChangedListener;
import com.hh.utility.PuUtils;

/**<br>
 * A dialog showing a color palette  with holo light style
 * 
 * @see See also :https://code.google.com/p/color-picker-view/
 * @author WajdiHh  : Last modification : 23/05/2013
 *
 */
public class UiDialogColorPicker extends Dialog implements android.view.View.OnClickListener  {


	private static int mDefaultColor=Color.BLUE;
	private OnColorPickChangedListener _mListener;
	private OnPickColorListener _mListenerOnPick;
	private int _mCurrentColor;
	private FrameLayout _mFrameCurrent,_mFrameSelected;
	private TextView _mTvTitle;
	private LinearLayout _mLlSeparator;
	private ColorPickerView _mColorView;
	private EditText _mEdCodeColor;
	private Resources _mRes;
	
	/**
	 * <br>
	 * Dialog constructor
	 * 
	 * @author WajdiHh
	 * @param context :  the context the view is running in
	 */
	public UiDialogColorPicker(Context context) {
		super(context);

		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_dialog_colorpicker);

		_mFrameCurrent=(FrameLayout) findViewById(R.id.uiDColorP_llCurrent);
		_mFrameSelected=(FrameLayout) findViewById(R.id.uiDColorP_llSelected);
		_mTvTitle=(TextView) findViewById(R.id.uiDColorP_TvTitle);
		_mLlSeparator=(LinearLayout) findViewById(R.id.uiDColor_Separator);
		_mEdCodeColor=(EditText) findViewById(R.id.uiDColorP_EdColor);

		_mColorView=(ColorPickerView) findViewById(R.id.myColorPickerView);

		_mColorView.setOnColorChangedListener(new OnColorChangedListener() {	
			@Override
			public void onColorChanged(int color) {
				if(_mListener!=null)
					_mListener.colorChanged(color);

				updateColorView(color);
			}
		});
		this.setCanceledOnTouchOutside(true);	

		Button lBtnValidate=(Button) findViewById(R.id.uiDColor_BtnValidate);
		lBtnValidate.setOnClickListener(this);
		Button lNewColor=(Button) findViewById(R.id.uiDColorP_BtnNewColor);
		lNewColor.setOnClickListener(this);
		Button lBtnCancel=(Button) findViewById(R.id.uiDColor_BtnCancel);
		lBtnCancel.setOnClickListener(this);
		
		_mRes= getContext().getResources();
	}

	/**<br>
	 * Used to set DEFAULT color
	 * 
	 * @author WajdiHh
	 * @param pColor :color integer code
	 */
	public void setColor(int pColor){
		mDefaultColor=pColor;
	}
	/** <br>
	 * Register a callback to be invoked when color is picked clicking on validate button
	 * 
	 * @author WajdiHh
	 * @param pListener: the listener to handle pick color
	 * @see OnPickColorListener
	 */
	public void setOnPickColor(OnPickColorListener pListener){
		_mListenerOnPick=pListener;
	}
	/**
	 * <br>
	 * Register a callback to be invoked when color is changed touching the color palette.
	 * 
	 * @author WajdiHh
	 * @param pListener: the listener to handle changing color
	 * @see OnColorChangedListener
	 */
	public void setOnColorPickChangedListener(OnColorPickChangedListener pListener){
		_mListener=pListener;
	}
	
	private void updateColorView(int pColor){

		_mFrameCurrent.setBackgroundColor(pColor);
		_mTvTitle.setTextColor(pColor);
		_mLlSeparator.setBackgroundColor(pColor);
		_mEdCodeColor.setText(UiUtility.colorConvertIntegerToHex(pColor).replace("#", ""));
		_mCurrentColor=pColor;
	}
	@Override
	public void onAttachedToWindow() {
		super.onAttachedToWindow();

		updateColorView(mDefaultColor);
		_mFrameSelected.setBackgroundColor(mDefaultColor);
		_mColorView.setColor(mDefaultColor);
	}

	@Override
	public void onClick(View v) {
		
		int lID=v.getId();
		if(lID==R.id.uiDColor_BtnValidate){
			mDefaultColor=_mCurrentColor;
			if(_mListenerOnPick!=null)
				_mListenerOnPick.onPick(mDefaultColor);
			_mFrameSelected.setBackgroundColor(mDefaultColor);
			dismiss();
		}
		else if(lID==R.id.uiDColorP_BtnNewColor){
			
			UiUtility.clearAllChildrensFocus(_mFrameCurrent);
			PfKeyboard.hide(getContext(), v);

			String lColorS="#"+_mEdCodeColor.getText().toString();

			boolean lIsColorValidated = PuUtils.isValidHex(lColorS);

			if(lColorS.length()==7 && lIsColorValidated){

				int lColor=UiUtility.colorConvertHexToInteger(lColorS);
				updateColorView(lColor);
				_mColorView.setColor(lColor);
				
			}else{
				Toast.makeText(getContext(),_mRes.getString(R.string.msgErr_invalidHexColor), Toast.LENGTH_SHORT).show();
				_mEdCodeColor.setText("");
			}

		}else if(lID==R.id.uiDColor_BtnCancel){
			dismiss();
		}
		
	}
}

